package com.qst.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.order.domain.OrderPay;
import com.qst.order.mapper.OrderPayMapper;
import com.qst.order.service.OrderPayService;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
@RequiredArgsConstructor
public class orderPayServiceImpl extends ServiceImpl<OrderPayMapper, OrderPay> implements OrderPayService {

    @Autowired
    private OrderPayMapper orderPayMapper;

    @Override
    public IPage<OrderPay> queryUserFlows(Integer userId, Integer page, Integer pageSize, LocalDateTime startDate, LocalDateTime endDate) {
        Page<OrderPay> pageParam = new Page<>(page, pageSize);

        // 2. 构建查询条件（核心：只能查询当前用户的流水，避免越权）
        QueryWrapper<OrderPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId); // 强制筛选当前用户


        // 4. 时间范围筛选（如传入开始时间，查询>=开始时间的数据）
        if (startDate != null) {
            queryWrapper.ge("pay_time", startDate);
        }
        // 如传入结束时间，查询<=结束时间的数据（注意：包含当天23:59:59）
        if (endDate != null) {
            queryWrapper.le("pay_time", endDate.plusDays(1).minusNanos(1));
        }

        // 5. 按交易时间倒序（最新的流水排在前面）
        queryWrapper.orderByDesc("pay_time");

        // 6. 分页查询
        return orderPayMapper.selectPage(pageParam, queryWrapper);
    }

}
